################################## 
############ Preamble ############ 
################################## 

# set language to English
Sys.setenv(LANG = "en")

# clean up
rm(list = ls())

# Set working directory: please set your own
#setwd("~/Dropbox/JOP third submission/JOP replication/")

# Load necessary packages
library(tidyverse)
library(haven)
library(psych)


# Import dataset
data <- read.dta13("01_data/elections_ciutadella.dta")

# Make the plot
turnout_trends_plot_period <- data %>%
  mutate(Election = ifelse(ep == 1, "European", "Other")) %>%
  ggplot(aes(x = period, y = turnout, color = Election)) +
  geom_point(position = position_dodge(width = 0.6), alpha = 0.1) +
  scale_color_manual(values = c("blue", "red")) +
  stat_summary(fun = mean, geom = "point", position = position_dodge(width = 0.05)) +
  stat_summary(fun = mean, geom = "line", position = position_dodge(width = 0.05)) +
  scale_x_continuous(breaks = c(1, 2, 3), 
                     labels = c("2 elections before (2009-11)", "1 election before (2014-15)", "2019")) +
  scale_y_continuous(limits = c(0, 80)) +
  geom_vline(xintercept = 2.5, linetype = 2, color = "black") +
  theme_bw() +
  labs(y = " ", x = " ") 
turnout_trends_plot_period

ggsave("04_plots/figured5.png", plot = turnout_trends_plot_period, width = 20, height = 15, units = "cm")